#!/bin/sh

#  ./test.sh expr sub_expr result_expected
#
# Expression may contain reference to weak variable sub_expr 
# If referenced in expr, 'sub_expr' should be computable

kmodule_name="kedr_calc_test_weak_vars"
kmodule="${kmodule_name}.ko"
result_file="/sys/module/${kmodule_name}/parameters/result"

if test $# -ne 3; then
    printf "Usage: ./test.sh expr sub_expr result_expected\n"
    exit 1
fi

expr="$1"
sub_expr="$2"
result_expected="$3"

@INSMOD@ "$kmodule" "expr=\"$expr\"" "sub_expr=\"$sub_expr\""
if test $? -ne 0; then
    printf "Failed to insert module into the kernel\n"
    exit 1
fi

result=`cat "$result_file"`

@RMMOD@ $kmodule
if test $? -ne 0; then
    printf "Failed to remove module from the kernel\n"
    exit 1
fi

if test "$result_expected" != "$result"; then
    printf "Expected result of evaluation of the expression is %s, but the module writes %s\n" "$result_expected" "$result"
    exit 1
fi